cmake_minimum_required(VERSION 3.5.0)
project(BevFusion
        LANGUAGES C CXX CUDA
        VERSION 0.0.1)

find_package(Boost 1.78 REQUIRED)
find_package(log4cxx REQUIRED)
find_package(Threads REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(PCL REQUIRED)
find_package(CUDA ${CUDA_VERSION} REQUIRED)
find_package(VTK REQUIRED)
    
find_package(PerceptionDataset REQUIRED PATHS ${PROJECT_SOURCE_DIR}/dep/perception)
find_package(PerceptionMap REQUIRED PATHS ${PROJECT_SOURCE_DIR}/dep/perception)

set(LIB_TARGET_NAME perception_e2e)
set(TARGET_NAME e2e)
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install)


set(TARGET_OUT_DIR ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

set(CMAKE_SKIP_BUILD_RPATH FALSE)  
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)  
set(CMAKE_INSTALL_RPATH "${ORIGIN}/lib")  
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

string(TIMESTAMP COMPILE_TIME "%Y-%m-%d %H:%M")

configure_file(
  version.h.in
  generated/version.h
  @ONLY
  )

############################################################################################

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})

include(CPack)

EXECUTE_PROCESS(COMMAND arch
        TIMEOUT 5
        OUTPUT_VARIABLE ARCH_TYPE
        OUTPUT_STRIP_TRAILING_WHITESPACE
        )

MESSAGE(STATUS "BUILD ARCH_TYPE ${ARCH_TYPE}")

set(TARGET_DEBUG_POSTFIX _debug CACHE STRING "suffix for debug builds")

################cuda#######################
if(NOT DEFINED CMAKE_CUDA_STANDARD)
    set(CMAKE_CUDA_STANDARD 17)
    set(CMAKE_CUDA_STANDARD_REQUIRED ON)
endif()

if (DEFINED GPU_ARCHS)
  message(STATUS "GPU_ARCHS defined as ${GPU_ARCHS}. Generating CUDA code for SM ${GPU_ARCHS}")
  separate_arguments(GPU_ARCHS)
else()
  list(APPEND GPU_ARCHS
      70
      72
    )

  string(REGEX MATCH "aarch64" IS_ARM "${ARCH_TYPE}")
  if (IS_ARM)
    # Xavier (SM72) only supported for aarch64.
    list(APPEND GPU_ARCHS 72)
  endif()

  if (CUDA_VERSION VERSION_GREATER_EQUAL 11.0)
    # Ampere GPU (SM80) support is only available in CUDA versions > 11.0
    list(APPEND GPU_ARCHS 80)
  endif()
  if (CUDA_VERSION VERSION_GREATER_EQUAL 11.1)
    list(APPEND GPU_ARCHS 86)
  endif()
    if (CUDA_VERSION VERSION_GREATER_EQUAL 11.4)
    list(APPEND GPU_ARCHS 87)
  endif()
  message(STATUS "GPU_ARCHS is not defined. Generating CUDA code for default SMs: ${GPU_ARCHS}")
endif()

foreach(arch ${GPU_ARCHS})
  set(GENCODES "${GENCODES} -gencode arch=compute_${arch},code=sm_${arch}")
endforeach()

# list(GET GPU_ARCHS -1 LATEST_SM)
# set(GENCODES "${GENCODES} -gencode arch=compute_${LATEST_SM},code=sm_${LATEST_SM}")
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
  set(CMAKE_CUDA_ARCHITECTURES ${GPU_ARCHS})
endif()

set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler -Wno-deprecated-declarations --expt-relaxed-constexpr --expt-extended-lambda")
##########################################

#############tensorrt info##############
file(STRINGS "/usr/include/${ARCH_TYPE}-linux-gnu/NvInferVersion.h" VERSION_STRINGS REGEX "#define NV_TENSORRT_.*")

foreach(TYPE MAJOR MINOR PATCH BUILD)
    string(REGEX MATCH "NV_TENSORRT_${TYPE} [0-9]" TRT_TYPE_STRING ${VERSION_STRINGS})
    string(REGEX MATCH "[0-9]" TRT_${TYPE} ${TRT_TYPE_STRING})
endforeach(TYPE)

foreach(TYPE MAJOR MINOR PATCH)
    string(REGEX MATCH "NV_TENSORRT_SONAME_${TYPE} [0-9]" TRT_TYPE_STRING ${VERSION_STRINGS})
    string(REGEX MATCH "[0-9]" TRT_SO_${TYPE} ${TRT_TYPE_STRING})
endforeach(TYPE)

set(TRT_VERSION "${TRT_MAJOR}.${TRT_MINOR}.${TRT_PATCH}" CACHE STRING "TensorRT project version")
set(ONNX2TRT_VERSION "${TRT_MAJOR}.${TRT_MINOR}.${TRT_PATCH}" CACHE STRING "ONNX2TRT project version")
set(TRT_SOVERSION "${TRT_SO_MAJOR}" CACHE STRING "TensorRT library so version")

message(STATUS "Building for TensorRT version: ${TRT_VERSION}, library version: ${TRT_SOVERSION}, \
onnx2trt version: ${ONNX2TRT_VERSION}")
########################################




include_directories(
  ${PROJECT_SOURCE_DIR}/include
  ${PROJECT_SOURCE_DIR}/include/common
  ${PERCEPTION_DATASET_INCLUDE_DIR}
  ${PERCEPTION_MAP_INCLUDE_DIR}
  ${CMAKE_BINARY_DIR}
  ${Boost_INCLUDE_DIR}
  ${EIGEN3_INCLUDE_DIR}
  ${OpenCV_INCLUDE_DIRS}
  ${CUDA_INCLUDE_DIRS}
  ${VTK_PREFIX_PATH}/include/vtk-${VTK_MAJOR_VERSION}.${VTK_MINOR_VERSION}
  ${PCL_INCLUDE_DIRS}
)

add_subdirectory(${PROJECT_SOURCE_DIR}/src)
add_subdirectory(${PROJECT_SOURCE_DIR}/test)
